package com.excesys.exsecs.access.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.excesys.exsecs.access.entity.AccessProduct;
import com.excesys.exsecs.access.service.AccessProductService;
import com.excesys.exsecs.common.core.util.R;
import com.excesys.exsecs.common.log.annotation.SysLog;
import com.excesys.exsecs.access.entity.AccessCategory;
import com.excesys.exsecs.access.service.AccessCategoryService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.excesys.exsecs.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 品类表
 *
 * @author pig
 * @date 2024-05-06 17:43:36
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/accessCategory" )
@Tag(description = "accessCategory" , name = "品类表管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class AccessCategoryController {

    private final AccessCategoryService accessCategoryService;
	private final AccessProductService accessProductService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param accessCategory 品类表
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('access_accessCategory_view')" )
    public R getAccessCategoryPage(@ParameterObject Page page, @ParameterObject AccessCategory accessCategory) {
        return R.ok(accessCategoryService.page(page, Wrappers.<AccessCategory>lambdaQuery()
				.like(StringUtils.isNotBlank(accessCategory.getName()), AccessCategory::getName, accessCategory.getName())));
    }

	/**
	 * 列表
	 * @param accessCategory 品类表
	 * @return
	 */
	@Operation(summary = "列表" , description = "列表" )
	@GetMapping("/list" )
	//@PreAuthorize("@pms.hasPermission('access_accessCategory_view')" )
	public R list(@ParameterObject AccessCategory accessCategory) {
		return R.ok(accessCategoryService.list(Wrappers.<AccessCategory>lambdaQuery()
				.like(StringUtils.isNotBlank(accessCategory.getName()), AccessCategory::getName, accessCategory.getName())));
	}


    /**
     * 通过id查询品类表
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('access_accessCategory_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(accessCategoryService.getById(id));
    }

    /**
     * 新增品类表
     * @param accessCategory 品类表
     * @return R
     */
    @Operation(summary = "新增品类表" , description = "新增品类表" )
    @SysLog("新增品类表" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('access_accessCategory_add')" )
    public R save(@RequestBody AccessCategory accessCategory) {
        return R.ok(accessCategoryService.save(accessCategory));
    }

    /**
     * 修改品类表
     * @param accessCategory 品类表
     * @return R
     */
    @Operation(summary = "修改品类表" , description = "修改品类表" )
    @SysLog("修改品类表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('access_accessCategory_edit')" )
    public R updateById(@RequestBody AccessCategory accessCategory) {
        return R.ok(accessCategoryService.updateById(accessCategory));
    }

    /**
     * 通过id删除品类表
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除品类表" , description = "通过id删除品类表" )
    @SysLog("通过id删除品类表" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('access_accessCategory_del')" )
    public R removeById(@RequestBody Long[] ids) {
		//如果绑定了产品，则不能删除
		List<AccessCategory> categories = accessCategoryService.listByIds(CollUtil.toList(ids));
		List<String> Identifies = categories.stream().map(AccessCategory::getIdentify).collect(Collectors.toList());
		long count = accessProductService.count(Wrappers.<AccessProduct>lambdaQuery().in(AccessProduct::getCategory, Identifies));
		if (count > 0 ){
			return R.failed("已选品类已绑定产品，无法删除，如需删除品类，则先解绑产品");
		}
		return R.ok(accessCategoryService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param accessCategory 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('access_accessCategory_export')" )
    public List<AccessCategory> export(AccessCategory accessCategory,Long[] ids) {
        return accessCategoryService.list(Wrappers.lambdaQuery(accessCategory).in(ArrayUtil.isNotEmpty(ids), AccessCategory::getId, ids));
    }
}